<template>
  <ActionPanel :loading="loading">
    <el-form ref="formRef" :model="data" :rules="rules" label-width="auto">
      <el-form-item label="名称" prop="name">
        <el-input v-model="data.name" />
      </el-form-item>
      <el-form-item label="标签" prop="label">
        <el-input v-model="data.label" />
      </el-form-item>
      <el-form-item label="注释" prop="comment">
        <el-input type="textarea" v-model="data.comment" />
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-input v-model="data.version" />
      </el-form-item>
      <el-form-item label="版权" prop="copyright">
        <el-input v-model="data.copyright" />
      </el-form-item>
    </el-form>
    <template #footer>
      <ElButton type="primary" @click="save">保存</ElButton>
      <el-button @click="close">关闭</el-button>
    </template>
  </ActionPanel>
</template>
<script setup lang="ts">
  import { closePopupKey } from '@/components/Popup';
  import { App } from '@/views/types';
  import { ElButton, ElForm, ElFormItem, ElInput } from 'element-plus';
  import { inject, ref } from 'vue';
  import feedback from '@/utils/feedback';
  import { ActionPanel } from '@/components/ActionPanel';
  import { appApi } from '@/api';

  const emits = defineEmits(['save'])

  const formRef = ref()
  const close = inject<() => void>(closePopupKey, void 0)

  const data = ref<App>({})
  const rules = {
    name: { required: true, message: '请填写名称', trigger: 'blur' }
  }

  const loading = ref(false)
  const save = async () => {
    await formRef.value.validate(async (valid, fields) => {
      if (valid) {
        loading.value = true
        const res = await appApi.create(data.value)
        loading.value = false
        emits('save')
        close?.()
        feedback.msgSuccess(res.msg)
      } else {
        console.log('error submit!', fields)
      }
    })
  }

</script>